package cn.edu.dgut.css.sai.createspringbootprojectdemo.controller;

import cn.edu.dgut.css.sai.createspringbootprojectdemo.pojo.Address;
import cn.edu.dgut.css.sai.createspringbootprojectdemo.pojo.User;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: leesk
 * @Description:
 * @Date: Create in 16:14 2020/10/11
 */
@RequestMapping("user")
@RestController
public class UserController {
    @RequestMapping("/get")
    public User get(){

        List<Address> addressList=new ArrayList<>();
        addressList.add(new Address("广东省东莞市大学路1号","201841413418","18025534180"));
        addressList.add(new Address("广东省东莞市大学路2号","201841413433","18031232320"));
        User user = new User(1, "leesk",addressList);


        //01 基本类型解析
        Gson gson = new Gson();
        int i = gson.fromJson("99", int.class); //100
        double d = gson.fromJson("\"99.99\"", double.class);  //99.99
        boolean b = gson.fromJson("true", boolean.class);     // true
        String str = gson.fromJson("String", String.class);   // String
        System.out.println("int 类型："+i);
        System.out.println("double 类型："+d);
        System.out.println("boolean 类型："+b);
        System.out.println("String 类型："+str);

        //02 实体对象转 json str
        String rst=gson.toJson(user);
        System.out.println("实体对象转:");
        System.out.println(rst);

        //03 对象转实体 json fromJson
        User user2 = gson.fromJson(rst, User.class);
        System.out.println("对象转实体:");
        System.out.println(user2);

        return  user;
    }
}
